Method
- block of code that you write that you can call from anywhere, anytime if it is included in that class and folder.
- Block of code that does one specific task
- Separate blocks of code to do specific action
ex. setHeight(), move(int x, int y)
- Reasoning call the same code over and over
- Names for method are always like the lowerCase toStart Upper After.
- Methods are always named like an Action and Verbs.
- A java method is a series of statements that perform some repeated task. Instead of writing 10 lines of code we can put those ten lines in a method and just call it in one line. It is like a shortcut.
Concept:
Instead of just repeating the following over and over again...
System.out.println("Attn: AP Computer Science");
System.out.println("Woodbridge Senior High School");
System.out.println("3001 Old Bridge Road");
System.out.println("Woodbridge VA 22192");
...we could just put it in a method like this...
public static void printHeader()
{
System.out.println("Attn: AP Computer Science");
System.out.println("Woodbridge Senior High School");
System.out.println("3001 Old Bridge Road");
System.out.println("Woodbridge VA 22192");
}
...and then all we'd have to do is call it using the method name...
printHeader();
...so it'll still print the same thing:
Attn: AP Computer Science
Woodbridge Senior High School
3001 Old Bridge Road
Woodbridge VA 22192
How to write a Method
public static void main (String[]args)
{
// this is a method
// void - returns nothing, just does what it says to do
// main - the name of the method
// names for methods are always like this lowerCastToStartUpperAfter
// methods are always named like an action, verb
// methods do something: actions or verbs
// parameters or arguments are values that are being sent in
}
public <type><name>(<arguments/parameters>)
example:
public void move (int xChange, int yChange)
{
// the code that moves your whatever you want based on xChange and yChange
head.move(xChange, yChange);
}
an example of this move method would be:
move( 3, 3 );
which would move the object 3 to the right & 3 down
Types of Methods
Accessors: get info about your fields
-example: getHeight(), getNumBaths()
-example in code:
public class House
{
private int baths;
private int bedrooms;
//accessor methods
public int getNumBaths()
{
return baths;
}
public int getNumBedrooms()
{
return bedrooms;
}
}
Mutator: changes things
-example: setHeight(), move(x,y)
-example in code:**
public class House
{
private int numRooms;
private int x,y;
//mutator
public void setNumRooms(int Num)
{
numRooms = num;
}
public void move (int xChange, yChange)
{
x = x + xChange;
y = y + yChange;
}
}
Remember, the method you create only works for the one program it's in unless other programs use the original one. Trying to call the method in a different class will not work unless there is some relationship established between the two.
- block of code that you write that you can call from anywhere, anytime if it is included in that class and folder.
- Block of code that does one specific task
- Separate blocks of code to do specific action
ex. setHeight(), move(int x, int y)
- Reasoning call the same code over and over
- Names for method are always like the lowerCase toStart Upper After.
- Methods are always named like an Action and Verbs.
- A java method is a series of statements that perform some repeated task. Instead of writing 10 lines of code we can put those ten lines in a method and just call it in one line. It is like a shortcut.
Concept:
Instead of just repeating the following over and over again...
System.out.println("Attn: AP Computer Science"); System.out.println("Woodbridge Senior High School"); System.out.println("3001 Old Bridge Road"); System.out.println("Woodbridge VA 22192");...we could just put it in a method like this...public static void printHeader() { System.out.println("Attn: AP Computer Science"); System.out.println("Woodbridge Senior High School"); System.out.println("3001 Old Bridge Road"); System.out.println("Woodbridge VA 22192"); }...and then all we'd have to do is call it using the method name......so it'll still print the same thing:
Attn: AP Computer Science
Woodbridge Senior High School
3001 Old Bridge Road
Woodbridge VA 22192
How to write a Method
public static void main (String[]args) { // this is a method // void - returns nothing, just does what it says to do // main - the name of the method // names for methods are always like this lowerCastToStartUpperAfter // methods are always named like an action, verb // methods do something: actions or verbs // parameters or arguments are values that are being sent in } public <type><name>(<arguments/parameters>) example: public void move (int xChange, int yChange) { // the code that moves your whatever you want based on xChange and yChange head.move(xChange, yChange); }an example of this move method would be:move( 3, 3 );
which would move the object 3 to the right & 3 down
Types of Methods
Accessors: get info about your fields
-example: getHeight(), getNumBaths()
-example in code:
public class House { private int baths; private int bedrooms; //accessor methods public int getNumBaths() { return baths; } public int getNumBedrooms() { return bedrooms; } }Mutator: changes things
-example: setHeight(), move(x,y)
-example in code:**
public class House { private int numRooms; private int x,y; //mutator public void setNumRooms(int Num) { numRooms = num; } public void move (int xChange, yChange) { x = x + xChange; y = y + yChange; } }Remember, the method you create only works for the one program it's in unless other programs use the original one. Trying to call the method in a different class will not work unless there is some relationship established between the two.